<template>
<!--hotmall/pages/goodShop/goodShop.wxml-->
<view class="body">
  <!-- 头部 -->
  <view class="head">
    <view class="headimg">
      <image :src="logo" class="headimg"></image>
    </view>
    <view class="headtext">
      <view class="headtitle">
        <text class="name">{{name}}</text>
      </view>
      <view class="Sold">
        <text>商品数：{{length}} </text>
        <text>已售：0</text>
      </view>
    </view>
    <view>
    <button class="collection is_coll" @tap="collection" v-if="state == 0">收藏</button>
     <button class="collection not_coll" @tap="collection" v-if="state == 1">已收藏</button>
    </view>
  </view>
  <!-- 内容 -->
  <view class="body">
    <view class="body">
      <view class="nav bc_white">
        <view :class="selected?'red':'default'" @tap="selectedFun">店铺首页</view>
        <view :class="selected1?'red':'default'" @tap="selected1Fun">全部商品</view>
        <view :class="selected2?'red':'default'" @tap="selected2Fun">新品上架</view>
      </view>
      <view :class="selected?'show':'hidden'">
        <view class="bodyshop">
          <view v-for="(item, index) in shopList" :key="index" class="commodity" :data-index="index" @tap="detail">
            <view class="coimg">
              <image :src="item.thumb_img"></image>
            </view>
            <view class="cotext">
              <view class="cotitle">{{item.goods_name}}</view>
              <view class="comoney">￥{{item.market_price}}</view>
            </view>
          </view>
        </view>
        <view class="nodata" v-if="length == 0">
            <image src="/static/remao_shop/img/noData.png" class="nodataimg"></image>
            <view class="nodatatext">暂无数据</view>
          </view>
      </view>
      <view :class="selected1?'show':'hidden'">
        <view class="bodyshop">
          <view v-for="(item, index) in shopList" :key="index" class="commodity" :data-index="index" @tap="detail">
            <view class="coimg">
              <image :src="item.thumb_img"></image>
            </view>
            <view class="cotext">
              <view class="cotitle">{{item.goods_name}}</view>
              <view class="comoney">￥{{item.market_price}}</view>
            </view>
          </view>
        </view>
        <view class="nodata" v-if="length == 0">
            <image src="/static/remao_shop/img/noData.png" class="nodataimg"></image>
            <view class="nodatatext">暂无数据</view>
          </view>
      </view>
      <view :class="selected2?'show':'hidden'">
        <view class="bodyshop">
          <view v-for="(item, index) in shopList" :key="index" class="commodity" :data-index="index" @tap="detail">
            <view class="coimg">
              <image :src="item.thumb_img"></image>
            </view>
            <view class="cotext">
              <view class="cotitle">{{item.goods_name}}</view>
              <view class="comoney">￥{{item.market_price}}</view>
            </view>
          </view>
          <view class="nodata" v-if="length == 0">
            <image src="/static/remao_shop/img/noData.png" class="nodataimg"></image>
            <view class="nodatatext">暂无数据</view>
          </view>
        </view>
      </view>
    </view>
  </view>
</view>
</template>

<script>
var iswe7 = wx.getStorageSync("iswe7");
import api from "../../../api";
var app = getApp();

export default {
  data() {
    return {
      selected: true,
      selected1: false,
      selected2: false,
      id: 0,
      name: '',
      logo: '',
      type: 3,
      length: 0,
      shopList: [],
      index: 0,
      shopID: [],
      uid: 0,
      state: 0 // currentTab: 0
      ,
      currentTab: "",
      goodsimg: ""
    };
  },

  components: {},
  props: {},

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading({
      title: '加载中'
    });

    if (options.scene) {
      const scene = decodeURIComponent(options.scene);
      this.setData({
        id: options.scene,
        name: options.name,
        logo: options.logo
      });
    } else {
      this.setData({
        id: options.id,
        name: options.name,
        logo: options.logo
      });
    }

    this.getList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    setTimeout(function () {
      wx.hideLoading();
    }, 2000);
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.getStorage({
      key: 'userId',
      success: res => {
        console.log(res);
        this.setData({
          uid: res.data
        });
        console.log(this.uid);
        this.iscollection();
        this.iscollection();
      }
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  methods: {
    selectedFun: function (e) {
      wx.showLoading({
        title: '加载中'
      });
      this.setData({
        selected1: false,
        selected2: false,
        selected: true,
        type: 3
      });
      this.getList();
    },
    selected1Fun: function (e) {
      wx.showLoading({
        title: '加载中'
      });
      this.setData({
        selected: false,
        selected2: false,
        selected1: true,
        type: ''
      });
      this.getList();
    },
    selected2Fun: function (e) {
      wx.showLoading({
        title: '加载中'
      });
      this.setData({
        selected: false,
        selected2: true,
        selected1: false,
        type: 2
      });
      this.getList();
    },
    //滑动切换
    swiperTabView: function (e) {
      this.setData({
        currentTab: e.detail.current
      });
    },
    //点击切换
    clickTab: function (e) {
      if (this.currentTab === e.target.dataset.current) {
        return false;
      } else {
        this.setData({
          currentTab: e.target.dataset.current
        });
      }
    },

    detail(e) {
      console.log(e);
      var l = e.currentTarget.dataset.index;
      var id = this.shopID[l];
      console.log(this.goodsimg);
      wx.navigateTo({
        url: '../../packageA/rushdetails/rushdetails?goods_id=' + id + '&img=' + this.goodsimg
      });
    },

    //收藏店铺
    collection() {
      if (this.uid == 0) {
        if (iswe7 == 1) {
          wx.navigateTo({
            url: '../../packageA/login/login'
          });
        } else {
          wx.navigateTo({
            url: '../../packageA/logins/login'
          });
        }
      } else {
        app.globalData.request({
          url: api.default.collection,
          header: {
            'content-type': 'application/x-www-form-urlencoded' // 默认值

          },
          method: 'POST',
          data: {
            store_id: this.id,
            uid: this.uid
          },
          success: res => {
            console.log(res);
            wx.showToast({
              title: res.msg
            });

            if (res.msg == '收藏成功') {
              console.log("yes");
              this.setData({
                state: 1
              });
            } else {
              console.log('111');
              this.setData({
                state: 0
              });
            }
          }
        });
      }
    },

    iscollection() {
      app.globalData.request({
        url: api.default.collection,
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 默认值

        },
        method: 'POST',
        data: {
          store_id: this.id,
          uid: this.uid
        },
        success: res => {
          console.log(res);

          if (res.msg == '收藏成功') {
            console.log("2323232");
            this.setData({
              state: 1
            });
          } else {
            console.log('=========================>');
            this.setData({
              state: 0
            });
          }
        }
      });
    },

    getList() {
      app.globalData.request({
        url: api.default.shop_goods,
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 默认值

        },
        method: 'POST',
        data: {
          store_id: this.id,
          type: this.type,
          page: 1
        },
        success: res => {
          console.log(res);
          let id = [];
          var img = [];

          for (let i = 0; i < res.list.length; i++) {
            id = id.concat(res.list[i].id);
            img = res.list[i].thumb_img;
          }

          this.setData({
            goodsimg: img,
            shopList: res.list,
            length: res.list.length,
            shopID: id
          });
          wx.hideLoading();
          console.log(this.shopID);
        }
      });
    }

  }
};
</script>
<style>
@import "./goodShop.css";
</style>